#!/bin/bash
# Usage: ./set_madgraph_parameter.sh ptj 150 run_card.dat
# Usage: ./set_madgraph_parameter.sh htjmin 200 run_card.dat
# Usage: ./set_madgraph_parameter.sh htjmax 300 run_card.dat
# Usage: ./set_madgraph_parameter.sh run_tag abcdefg run_card.dat
# Usage: ./set_madgraph_parameter.sh nevents 50000 run_card.dat
# Usage: ./set_madgraph_parameter.sh ebeam1 4000 run_card.dat
# Usage: ./set_madgraph_parameter.sh ebeam2 4000 run_card.dat
# Usage: ./set_madgraph_parameter.sh maxjetflavor 5 run_card.dat
#
# # Usage: ./set_madgraph_parameter.sh aS 0.123 param_card.dat
#
# the target file has either the structure: 
#                                            150  = ptj       ! minimum pt for the jets 
#
# OR
#                                            3 1.184000e-01 # aS 
#                                         1010 4.000000e+02 # MassAxi
#                                          1000006 2.00e+02 # msu3
#
# OR
#                                   DECAY 1010 1.000000e+01 # WidAxi
#
#
#
################ Colors ################
ESC_SEQ="\x1b["
COL_RESET=$ESC_SEQ"39;49;00m"
RED=$ESC_SEQ"31;01m"
GREEN=$ESC_SEQ"32;01m"
YELLOW=$ESC_SEQ"33;01m"
BLUE=$ESC_SEQ"34;01m"
MAGENTA=$ESC_SEQ"35;01m"
CYAN=$ESC_SEQ"36;01m"
########################################
#
basefile=$3
num=$2

param=$1

if [ "$1" = "muF" ]; then
echo "Changing the FACTORIZATION scale"
param="facscale\:QCD\ fac\_scale\ choice"
fi

echo "Changing the file "$basefile
echo $num" = "$param""

#take whatever line contains ['runstring'] and substitute it with a whole new line made of $2 ['runstring']
if [ "$basefile" = "run_card.dat" ]; then
    cat $basefile | sed "s/.*\ =\ $param\ .*/\ $num\ \ =\ $param /g" > temptemp
fi
if  [ "$basefile" = "param_card.dat" ]; then
    pre=`cat $basefile | grep -e $param | rev | cut -d"#" -f2 | sed -e's/  */ /g' | cut -d" " -f3- | rev`
# OSX sed compatible    cat $basefile | sed "s/.*[[:<:]]$param[[:>:]]/$pre\ $num\ #\ $param /g" > temptemp
cat $basefile | sed "s/.*\<$param\>/$pre\ $num\ #\ $param /g" > temptemp
fi
cp $basefile $basefile.backup
cp temptemp $basefile
rm temptemp
echo -e $RED"changes in the file"$COL_RESET
diff $basefile.backup $basefile
echo -e $RED"end of changes in the file"$COL_RESET
